import sys

import pygame

from game import Game
from menu import Menu
from settings import Settings
from state import State


class BattleCity(object):
    # 游戏窗口
    _screen = None
    # 游戏设置
    _settings = None
    # 游戏状态
    _state = None

    # 初始化游戏进程
    def __init__(self):
        # 初始化display模块
        pygame.init()
        pygame.mixer.init()
        pygame.event.set_allowed([pygame.KEYDOWN, pygame.KEYUP, pygame.QUIT])
        # 游戏设置
        self._settings = Settings()
        self._state = State()

        # 初始化窗口
        self._screen = pygame.display.set_mode(
            (self._settings.screen_width, self._settings.screen_height), 0, 32)

        # 设置标题
        pygame.display.set_caption("坦克大战")
        self.start_sound = pygame.mixer.Sound('sounds/start.wav')

    # 开始游戏
    def play(self):
        menu = Menu(self._settings, self._state)
        game = Game(self._settings, self._state)
        self.start_sound.play(loops=0)

        while True:
            # 设置背景色
            self._screen.fill((0, 0, 0))
            if not self._state.active:
                # 绘制菜单
                menu.display(self._screen)
            else:
                # 显示游戏界面
                game.display(self._screen)
            # 刷新屏幕
            pygame.display.update()

    # 关闭游戏
    def stop_game(self):
        print('stop game')
        sys.exit()

    # 销毁pygame模块
    def __del__(self):
        pygame.quit()


if __name__ == '__main__':
    game = BattleCity()
    game.play()
